package com.md.youyouhui.ui.user;

import android.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityReplacePwdBinding;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

/**
 * Created by AdministratorZJ
 * on 2019/4/10.
 */
public class ReplacePwdActivity extends BaseActivity {
    private ActivityReplacePwdBinding pwdBinding;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        pwdBinding = DataBindingUtil.setContentView(this, R.layout.activity_replace_pwd);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(pwdBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
       pwdBinding.replaceBtn.setOnClickListener(v -> mobilePwd());
    }

    void mobilePwd(){
        String oldPwd = pwdBinding.replacePwd.getText().toString();
        if (TextUtils.isEmpty(oldPwd)) {
            Toast.makeText(ct, "请输入旧密码", Toast.LENGTH_SHORT).show();
            return;
        }
        String newPwd = pwdBinding.replaceNewPwd.getText().toString();
        if (TextUtils.isEmpty(newPwd)) {
            Toast.makeText(ct, "请输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }
        String newPwds = pwdBinding.replaceNews.getText().toString();
        if (TextUtils.isEmpty(newPwds)) {
            Toast.makeText(ct, "请再次输入新密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!newPwd.equals(newPwds)) {
            Toast.makeText(ct, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.mobilePwd(oldPwd, newPwd).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    pwdBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    pwdBinding.progress.setVisibility(View.GONE);
                    Toast.makeText(ct, "修改成功", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case ERROR:
                    pwdBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
